package gold.digger;

import gold.vo.TreeNode;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC623 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public TreeNode addOneRow(TreeNode root, int v, int d) {
            return addOneRowDFS(root, v, d, 3);
        }

        public TreeNode addOneRowDFS(TreeNode root, int v, int d, int direction) {
            if (d <= 1) {
                // direction has three value:
                // 1:root is its parent's left;
                // 2:root is its parent's right;
                // 3:root has no parents;
                TreeNode newRoot = new TreeNode(v);
                if (direction == 1) newRoot.left = root;
                else if (direction == 2) newRoot.right = root;
                else newRoot.left = root;
                return newRoot;
            }

            if (null != root) root.left = addOneRowDFS(root.left, v, d - 1, 1);
            if (null != root) root.right = addOneRowDFS(root.right, v, d - 1, 2);
            return root;
        }
    }

    public void run() {
        Solution solution = new Solution();
        TreeNode tr = TreeNode.createTree("[4,2,null,3,1]");
        System.out.println(solution.addOneRow(tr, 1, 3));
    }

    public static void main(String[] args) throws Exception {
        LC623 an = new LC623();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}

